package com.Jshop.order.listener;

import com.Jshop.common.utils.JsonUtils;
import com.Jshop.common.utils.MultiThreadUtils;
import com.Jshop.order.pojo.OrderTransfer;
import com.Jshop.order.service.SecOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.FutureTask;


/**
 * @Description 监听秒杀服务 下单消息
 * @Param
 * @return
 * @Date  2021/11/14
 **/
@Component
@RabbitListener(queues = "secKillQueue")
public class SecKillOrderMessageListener {


    @Resource
    private SecOrderService secOrderService;


    /**
     * 监听 mq的消息 在这里完成秒杀下单操作
     * @param message xml格式的消息
     */
    @RabbitHandler
    public void getMessage(String message) throws Exception {
        //秒杀订单关键信息
        OrderTransfer secKillOrderInfo = JsonUtils.parse(message, OrderTransfer.class);
//        System.out.println("监听到下单消息...." + secKillOrderInfo);
        //Todo 此处计划采用线程池进行多线程下单操作
        if (secKillOrderInfo != null){
            MultiThreadUtils.toCacheThreadPool(new FutureTask<Boolean>( () -> {
                return secOrderService.createSecOrder(secKillOrderInfo);
            }));

        }

    }

}
